package cn.tedu.oa.notice.service.impl;

import cn.tedu.oa.common.exception.ServiceException;
import cn.tedu.oa.common.service.impl.BaseServiceImpl;
import cn.tedu.oa.common.vo.JsonResult;
import cn.tedu.oa.notice.dao.NoticeDao;
import cn.tedu.oa.notice.entity.Notice;
import cn.tedu.oa.notice.service.NoticeService;
import cn.tedu.oa.sys.entity.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by UID on 2018/9/17.
 */
@Service
public class NoticeServiceImpl extends BaseServiceImpl<Notice> implements NoticeService{
    @Autowired
    private NoticeDao noticeDao;

    @Override
    public Notice getNoticeById(Integer id){
        if( id==null || id <1){
           return null;
        }
        return noticeDao.get(id);
    }

    /**
     * 验证条件异常
     * @param notice ( method 添加 / 修改)
     */
    public void validate(Notice notice, String method){
        if ( notice==null ){
            throw new ServiceException(method+"提交的公告信息不能为空！");
        }
        if ( notice.getType()==null ){
            throw new ServiceException(method+"公告类型不能为空!");
        }
        if ( notice.getTitle()==null ){
            throw new ServiceException(method+"公告标题不能为空!");
        }
        if ( notice.getContext()==null ){
            throw new ServiceException(method+"公告内容不能为空!");
        }
        if ( notice.getPublisher()==null){
            throw new ServiceException(method+"发布人不能为空!");
        }
        if ( notice.getDepartment()==null ){
            throw new ServiceException(method+"部门不能为空!");
        }
        if ( notice.getStatus()==null ){
            throw new ServiceException(method+"状态不能为空!");
        }
    }
    /**
     * 修改公告数据
     * @param notice
     */
    @Override
    public void updateNotice(Notice notice){
        validate(notice,"updateNotice");
        //设置修改时间
        notice.setAddTime(new Date());
        //设置备注（修改）
        notice.setRemark("update");
        noticeDao.updateNotice(notice);
    }

    @Override
    public JsonResult getDeptIdAndName() {
        JsonResult result = new JsonResult();
        List<Department> list = noticeDao.getDeptIdAndName();
        result.setData(list);
        return result;
    }

    /**
     * 分页数据查询
     * @param criteria       (查询条件)
     * @param pageNo         (当前第几页)
     * @param pageSize      （每页显示几行）
     * @return
     */
    @Override
    public List<Notice> getNoticePageList(String criteria, Integer pageNo, Integer pageSize) {
        if ( pageSize==null || pageSize<1){
            throw new IllegalArgumentException("参数错误,请重新选择!");
        }
        if (pageNo==null || pageNo<1){
            pageNo=1;
        }
        //开始查询第几条
        int startIndex=(pageNo-1)*pageSize+1;
        //查询结束条件
        int endIndex=startIndex+pageSize-1;
        //查询添加去左右空格 criteria.trim();
        if ( criteria!=null ){
        criteria=criteria.trim();
        }
        //执行查询操作
        List<Notice> list = noticeDao.getNoticePageList(criteria,startIndex,endIndex);
        if (list==null){
            throw new ServiceException("没有数据了，请先添加数据！");
        }
        return list;
    }

    /**
     * 添加数据
     * @param notice
     */
    @Override
    public void add(Notice notice){
        if(notice.getType()==null || notice.getType()<0){
            throw new ServiceException("类型不能为空！");
        }
        if(notice.getTitle()==null || notice.getTitle()==""){
            throw new ServiceException("标题不能为空！");
        }
        if(notice.getContext()==null || notice.getContext()==""){
            throw new ServiceException("内容不能为空！");
        }
        if(notice.getPublisher()==null || notice.getPublisher()==""){
            throw new ServiceException("发布人不能为空！");
        }
        if(notice.getDepartment()==null || notice.getDepartment()==""){
            throw new ServiceException("部门不能为空！");
        }
        if(notice.getStatus()==null ||notice.getStatus()<0){
            throw new ServiceException("状态不能为空！");
        }
        notice.setIsdel(0);
        notice.setStartTime(new Date());
        notice.setAddTime(new Date());
        noticeDao.add(notice);

    }
}
